//  Author: Nick
//  Date:23 Sep,2021

//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

//ESP32 Arduino驱动0.91" OLED 滚动显示
//NODEMCU-32 V1.2
//Arduino V1.8.16
//ESP32 Arduino驱动0.91" OLED
//NODEMCU-32 V1.2
//Arduino V1.8.16
//参考链接：
//滚动显示实验：https://blog.csdn.net/tonyiot/article/details/106091716
//未说明SDA和SCL引脚设定：http://www.taichi-maker.com/homepage/reference-index/display-reference-index/arduino-oled-application/
//SDA和SCL在ware.begin()中定义：https://blog.csdn.net/qq_42805977/article/details/107765966

#include <Wire.h>
#include <math.h>
// 引入驱动OLED0.91所需的库
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SENSOR_LEN 40

#define SCREEN_WIDTH 128 // 设置OLED宽度,单位:像素
#define SCREEN_HEIGHT 64 // 设置OLED高度,单位:像素

// 自定义重置引脚,虽然教程未使用,但却是Adafruit_SSD1306库文件所必需的
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int positionX = 0;

long data = 0;
int bais_data =0;
float speed = 0;
void setup()
{
    Serial.begin(115200);
    Serial.println();
    long a = micros();
    Wire.begin(/*SDA*/ 23, /*SCL*/ 22);
    // 初始化OLED并设置其IIC地址为 0x3C
    display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
    // 清除屏幕
    display.clearDisplay();
    // 设置字体颜色,白色可见
    display.setTextColor(WHITE);
    //设置字体大小
    display.setTextSize(2);
    //设置光标位置
    // display.setCursor(positionX, 0);
    // display.print("0.91 OLED");

    // display.setCursor(positionX, 10);
    // display.print("Data: ");
    // display.print("2021-9-23");

    // display.setCursor(positionX, 20);
    // display.print("Author: ");
    // display.print("Nick");
    display.display();
    pinMode(18, INPUT_PULLDOWN);
    pinMode(19, INPUT_PULLDOWN);
    delay(1000);
}

void loop()
{
    display.println("TestSpeed:");
    display.display();

    while (digitalRead(18) == 1)
    {
    }
    data = micros();
    // Serial.print("data1:");
    // Serial.println(data);
    while (digitalRead(19) == 1)
    {
    }
    bais_data = micros() - data;

    while (digitalRead(18) == 0)
    {
    }
    data = micros();
    // Serial.print("data1:");
    // Serial.println(data);
    while (digitalRead(19) == 0)
    {
    }
    data = micros() - data - bais_data + 4;


    Serial.print("data2:");
    Serial.println(data);
    Serial.print("data3:"); 
    Serial.println(data);

    speed = (SENSOR_LEN * pow(10, -3)) / (data * pow(10, -6));

    Serial.print("Speed:");
    Serial.println(speed);
    display.println(speed);
    display.display();
    while (1)
    {
    }

    //由右向左滚动
    //display.startscrollleft(0x00, 0x0F);
}
